package com.xinfu.newproject.ui.meter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.ResidentCertificationActivity;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.bean.certifition.ResidentBean;
import com.xinfu.newproject.bean.certifition.ResidentCertificationBean;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.bean.meter.bean.AddUserMeterBean;
import com.xinfu.newproject.bean.meter.bean.CheckMeterCodeBean;
import com.xinfu.newproject.bean.meter.bean.MeterListBean;
import com.xinfu.newproject.contants.Contants;
import com.xinfu.newproject.presenter.MeterPresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;
import com.xinfu.newproject.widget.CustomClickListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.contants.Constant.LOGIN;

public class QRCodeAddMeterActivity extends BaseActivity implements IView.IMeterView {

    @BindView(R.id.layout_next_step_tv)
    TextView nextTv;

    @BindView(R.id.activity_qr_code_add_meter_project)
    TextView projectTv;

    @BindView(R.id.activity_qr_code_add_meter_et)
    EditText meterIdEt;

    private MeterPresenter meterPresenter = new MeterPresenter(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr_code_add_meter);
        ButterKnife.bind(this);
        registerBack();
        initViewBind();
        initLisener();
    }

    private void initLisener() {
        nextTv.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                if (isLogin()) {
                    if (projectTv.getText().toString().equals("请选择小区") || TextUtils.isEmpty(meterIdEt.getText())) {
                        BToast.showText(QRCodeAddMeterActivity.this, "请填写完整信息");
                    } else {
                        meterPresenter.chechMeterCode();
                    }
                }
            }

            @Override
            protected void onFastClick() {

            }
        });
    }

    private void initViewBind() {
        nextTv.setText("添加");
        setActivityTitle("扫码添加");
    }

    @OnClick({R.id.activity_qr_code_add_meter_project})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_qr_code_add_meter_project:
                Bundle bundle = new Bundle();
                bundle.putInt("type", Contants.CHOOSECOMMUNITYCERTIFITION);
                startActivityForResult(new Intent(this, ResidentCertificationActivity.class).putExtras(bundle), Contants.CHOOSECOMMUNITYCERTIFITION);
                break;
        }
    }

    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName;

    private boolean isLogin() {
        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(this, LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            return false;
        }
        return true;
    }

    @Override
    public String header() {
        return tenantId;
    }

    @Override
    public String userId() {
        return userId;
    }

    @Override
    public String meterCode() {
        return meterIdEt.getText().toString().trim();
    }

    @Override
    public String meterType() {
        return getIntent().getExtras().getString("type");
    }

    @Override
    public String projectId() {
        return projectId;
    }

    String meterId;

    @Override
    public String meterId() {
        return meterId;
    }

    @Override
    public void showToast(String content) {
        if (!content.contains("网络异常")) {
            BToast.showText(this, content);
        }
    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void showProgress() {
        showLoding();
    }

    @Override
    public void hideProgress() {
        hideLoding();
    }

    @Override
    public void getData(AddUserMeterBean addUserMeterBean) {
        BToast.showText(this, addUserMeterBean.getMessage());
        finish();
    }

    @Override
    public void getData(CheckMeterCodeBean checkMeterCodeBean) {
        if (checkMeterCodeBean.getResult() != null) {
            tenantId = checkMeterCodeBean.getResult().getTenantId();
            projectId = checkMeterCodeBean.getResult().getProjectId();
            meterId = checkMeterCodeBean.getResult().getId();
            meterPresenter.addUserMeter();
        }
    }

    @Override
    public void getData(MeterListBean meterListBean) {

    }

    ResidentBean residentBean;
    String projectId, tenantId;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case Contants.CHOOSECOMMUNITYCERTIFITION:
                    if (data.getExtras().getString("community") != null) {
                        residentBean = new Gson().fromJson(data.getExtras().getString("community"), ResidentBean.class);
                        ResidentCertificationBean.ResultBean.RoomCheckUserListBean video = (ResidentCertificationBean.ResultBean.RoomCheckUserListBean) residentBean.t;
                        projectId = video.getProjectId();
                        tenantId = String.valueOf(video.getTenantId());
                        projectTv.setText(data.getExtras().getString("title"));
                    }
                    break;
            }
        }
    }
}
